GNSS_ON=29
VCC3V3_EN=24
MCU_ON_RUN=31
MCU_NET_ON=27
GNSS_FLAG=26
MCU_BEEP=30

--gpio.setup(20, function(val) print("IRQ_20",val) end, gpio.PULLUP)
-- sys.taskInit(function()
--    sys.wait(1000) -- 免得日志刷了, 生产环境不需要
function db_int()
    -- 检查一下当前固件是否支持fdb
    if not fdb then
        while true do
            log.info("fdb", "this demo need fdb")
            sys.wait(1000)
        end
    end

    -- 初始化kv数据库
    fdb.kvdb_init("onchip_flash")
    log.info("fdb", "init complete")

    -- 先放入一堆值
    local bootime = fdb.kv_get("boottime")
    if bootime == nil or type(bootime) ~= "number" then
        bootime = 0
    else
        bootime = bootime + 1
    end

    fdb.kv_set("boottime", bootime)

    local phone_num = fdb.kv_get("phone_num")
    if phone_num == nil or type(phone_num) ~= "string" then
        fdb.kv_set("phone_num", "17620356231")
    end

    local server = fdb.kv_get("server")
    if server == nil or type(server) ~= "string" then
        fdb.kv_set("server", "budong666ni.eicp.net")
    end

    local province = fdb.kv_get("province")
    if province == nil or type(province) ~= "number" then
        fdb.kv_set("province", 0x00)
    end

    local city = fdb.kv_get("city")
    if city == nil or type(city) ~= "number" then fdb.kv_set("city", 0x00) end

    local company = fdb.kv_get("company")
    if company == nil or type(company) ~= "string" then
        fdb.kv_set("company", "cnostar")
    end

    local model = fdb.kv_get("model")
    if model == nil or type(model) ~= "string" then
        fdb.kv_set("model", "CT10")
    end

    local SN = fdb.kv_get("SN")
    if SN == nil or type(SN) ~= "string" then
        fdb.kv_set("SN", "20221221001")
    end

    local color = fdb.kv_get("color")
    if color == nil or type(color) ~= "number" then fdb.kv_set("color", 0) end

    local car_id = fdb.kv_get("car_id")
    if car_id == nil or type(car_id) ~= "string" then
        fdb.kv_set("car_id", "B52VP8")
    end

    log.info("fdb", "boottime: ", fdb.kv_get("boottime"))
    log.info("fdb", "phone_num: ", fdb.kv_get("phone_num"))
    log.info("fdb", "server: ", fdb.kv_get("server"))

    log.info("fdb", "province: ", fdb.kv_get("province"))
    log.info("fdb", "city: ", fdb.kv_get("city"))
    log.info("fdb", "company: ", fdb.kv_get("company"))
    log.info("fdb", "model: ", fdb.kv_get("model"))
    log.info("fdb", "SN: ", fdb.kv_get("SN"))
    log.info("fdb", "color: ", fdb.kv_get("color"))
    log.info("fdb", "car_id: ", fdb.kv_get("car_id"))
    -- 清空
    -- fdb.kv_clr()
    --[[
    -- 先放入一堆值
    local bootime = fdb.kv_get("boottime")
    if bootime == nil or type(bootime) ~= "number" then
        bootime = 0
    else
        bootime = bootime + 1
    end
    fdb.kv_set("boottime", bootime)

    fdb.kv_set("my_bool", true)
    fdb.kv_set("my_int", 123)
    fdb.kv_set("my_number", 1.23)
    fdb.kv_set("my_str", "luatos")
    fdb.kv_set("my_table", {name="wendal",age=18})

    fdb.kv_set("my_str_int", "123")
    fdb.kv_set("1", "123") -- 单字节key
    --fdb.kv_set("my_nil", nil) -- 会提示失败,不支持空值


    log.info("fdb", "boottime",      type(fdb.kv_get("boottime")),    fdb.kv_get("boottime"))
    log.info("fdb", "my_bool",      type(fdb.kv_get("my_bool")),    fdb.kv_get("my_bool"))
    log.info("fdb", "my_int",       type(fdb.kv_get("my_int")),     fdb.kv_get("my_int"))
    log.info("fdb", "my_number",    type(fdb.kv_get("my_number")),  fdb.kv_get("my_number"))
    log.info("fdb", "my_str",       type(fdb.kv_get("my_str")),     fdb.kv_get("my_str"))
    log.info("fdb", "my_table",     type(fdb.kv_get("my_table")),   json.encode(fdb.kv_get("my_table")))
    log.info("fdb", "my_str_int",     type(fdb.kv_get("my_str_int")),   fdb.kv_get("my_str_int"))
    log.info("fdb", "1 byte key",     type(fdb.kv_get("1")),   json.encode(fdb.kv_get("1")))

    -- 删除测试
    fdb.kv_del("my_bool")
    local t = fdb.kv_get("my_bool")
    log.info("fdb", "my_bool",      type(t),    t)
]] --
end

function db_set_ntrip(np)
    fdb.kv_set("nip", np[2])
    fdb.kv_set("nport", np[3])
    fdb.kv_set("nmp", np[4])
    fdb.kv_set("nun", np[5])
    fdb.kv_set("npw", np[6])
    fdb.kv_set("nup", np[7])

--    log.info("fdb", "nip: ", fdb.kv_get("nip"))
--    log.info("fdb", "nport: ", fdb.kv_get("nport"))
--    log.info("fdb", "nmp: ", fdb.kv_get("nmp"))
--    log.info("fdb", "nun: ", fdb.kv_get("nun"))
--    log.info("fdb", "npw: ", fdb.kv_get("npw"))
end

function db_set_jts(np)
    fdb.kv_set("jip", np[2])
    fdb.kv_set("jport", np[3])
--    log.info("fdb", "jip: ", fdb.kv_get("jip"))
--    log.info("fdb", "jport: ", fdb.kv_get("jport"))
end

function hw_int()
    -- gpio.setup(27, 0, gpio.PULLUP)
    gpio.setup(GNSS_ON, 0, gpio.PULLUP)  --GNSS_ON
    gpio.set(GNSS_ON, gpio.HIGH);

    gpio.setup(VCC3V3_EN, 0, gpio.PULLDOWN)    --VCC3V3_EN
    gpio.set(VCC3V3_EN, gpio.HIGH);

    gpio.setup(MCU_ON_RUN, 0, gpio.PULLUP) --MCU_ON_RUN
    gpio.set(MCU_ON_RUN, gpio.HIGH);

    gpio.setup(MCU_NET_ON, 0, gpio.PULLUP)  --MCU_NET_ON
    gpio.setup(GNSS_FLAG, 0, gpio.PULLUP)  --RTK

    gpio.setup(MCU_BEEP, 0, gpio.PULLUP) --BEEP
    gpio.set(MCU_BEEP, gpio.HIGH);
    --gpio.setup(20, function(val) print("IRQ_20",val) end, gpio.PULLUP)
end